//
//  OAExpendViewController.m
//  OASystem
//
//  Created by zhoupengfei on 15/12/6.
//  Copyright © 2015年 zpf. All rights reserved.
//
#import "detailViewController.h"
#import "OAExpendViewController.h"
#import "MBProgressHUD.h"
#import "HttpTools.h"
#import "CountyDepartmentListModel.h"
#import "HeaderView.h"
#import "OANewEmailController.h"
#import "OANewSMSViewController.h"
@interface OAExpendViewController ()<HeaderViewDelegate2>
@property(nonatomic,strong)NSMutableArray * groupArray;
@property(nonatomic,strong)NSMutableArray * subArray;
@end

@implementation OAExpendViewController

-(NSMutableArray*)groupArray{
    
    if (_groupArray == nil) {
        _groupArray = [NSMutableArray array];
    }
    
    return _groupArray;
}

-(NSMutableArray*)subArray{
    
    if (_subArray == nil) {
        _subArray = [NSMutableArray array];
    }
    
    return _subArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择人员";
    [self setRightItem];
    
    NSString * method = nil;
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    
    
    NSArray * array = nil;
    if (self.type == CountyDataType) {
        method = KDepTree;
        if (self.identifierString) {
               array = @[@{@"path":self.identifierString},@{@"userId":userId}];
        }else{
               array = @[@{@"userId":userId}];
        }
     
    }else if (self.type == UnitDataType){
        method = KLocalTree;
        array = @[@{@"userId":userId}];
    }else if(self.type == DepartmentDataType){
        method = KsTree;
        array = @[@{@"userId":userId}];
    }
    
    [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
   
    [HttpTools requestWithURL:KExchTree arrays:array method:method success:^(id JSON) {
    NSLog(@"%@",JSON);
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        NSArray * data = JSON[@"data"];
        if (data.count) {
            NSDictionary * ditc = [data firstObject];
            [self.contentsArray addObjectsFromArray:[CountyDepartmentListModel dataWithArray:ditc[@"PartNode"]]];
            
            self.groupArray = [self groups];
            
            for (CountyDepartmentListModel * model in self.groupArray) {
                NSMutableArray * subArray = [NSMutableArray array];
                for (CountyDepartmentListModel * listModel in self.contentsArray) {
                    if ([model.depid isEqualToString:listModel.depid] && listModel.bmmc.length ==0) {
                        [subArray addObject:listModel];
                        model.subArrays = subArray;
                    }
                }
            }
            
            [self.tableView reloadData];
        }
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
    }];
}

-(void)setRightItem{
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(sendAction)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
}

-(void)sendAction{

    NSMutableArray * tempArray = [NSMutableArray array];
    for (CountyDepartmentListModel * model in self.groupArray) {
        for (CountyDepartmentListModel * listModel in model.subArrays) {
            if (listModel.isSelected) {
                [tempArray addObject:listModel.ryxm];
                
            }
        }
    }
    
     [[NSNotificationCenter defaultCenter] postNotificationName:@"PostSelectNameNoti" object:nil userInfo:@{@"SelectName":tempArray}];
    
    for (UIViewController * VC in self.navigationController.viewControllers) {
        if ([VC isKindOfClass:[OANewEmailController class]] || [VC isKindOfClass:[OANewSMSViewController class]]) {
            [self.navigationController popToViewController:VC animated:YES];
            break;
        }
    }
    
}

-(void)clickHeaderView:(NSInteger)index{
     CountyDepartmentListModel * listModel = self.groupArray[index];
    listModel.isOpen = !listModel.isOpen;
    [self.tableView reloadData];
}

-(NSMutableArray*)groups{
    NSMutableArray * tempArray = [NSMutableArray array];
    for (CountyDepartmentListModel * listModel in self.contentsArray) {
        if (listModel.bmmc.length !=0) {
            [tempArray addObject:listModel];
        }
    }
    
    
    return tempArray;
}


#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.groupArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        CountyDepartmentListModel * listModel = self.groupArray[section];
    return   listModel.isOpen ? listModel.subArrays.count : 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString * cellid = @"CellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
    }
    CountyDepartmentListModel * listModel = self.groupArray[indexPath.section];
     CountyDepartmentListModel * subModel = listModel.subArrays[indexPath.row];
    cell.textLabel.text = subModel.ryxm;
    if (subModel.isSelected) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    HeaderView * headView = [HeaderView headerViewWithTableView:tableView];
    headView.delegate2 = self;
     CountyDepartmentListModel * listModel = self.groupArray[section];
    headView.bgButton.tag = section;
    [headView.bgButton setTitle:listModel.bmmc forState:UIControlStateNormal];
     headView.bgButton.imageView.transform = listModel.isOpen ? CGAffineTransformMakeRotation(M_PI_2) : CGAffineTransformIdentity;
    return headView;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CountyDepartmentListModel * listModel = self.groupArray[indexPath.section];
    CountyDepartmentListModel * subModel = listModel.subArrays[indexPath.row];
    subModel.isSelected = !subModel.isSelected;
  
    [self.tableView reloadData];

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}




@end
